package com.eshop.tools.rest;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.PostMapping;
import com.eshop.tools.domain.Picture;
import com.eshop.utils.SecurityUtils;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.List;
import com.eshop.tools.service.dto.PictureDto;
import javax.servlet.http.HttpServletResponse;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.security.access.prepost.PreAuthorize;
import com.eshop.logging.aop.log.Log;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.data.domain.Pageable;
import com.eshop.tools.service.dto.PictureQueryCriteria;
import com.eshop.tools.service.PictureService;
import com.eshop.dozer.service.IGenerator;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping({ "/api/pictures" })
@Api(tags = { "工具：免费图床管理" })
public class PictureController
{
    private final IGenerator generator;
    private final PictureService pictureService;
    
    public PictureController(final IGenerator generator, final PictureService pictureService) {
        this.generator = generator;
        this.pictureService = pictureService;
    }
    
    @Log("查询图片")
    @PreAuthorize("@el.check('pictures:list')")
    @GetMapping
    @ApiOperation("查询图片")
    public ResponseEntity<Object> getRoles(final PictureQueryCriteria criteria, final Pageable pageable) {
        return (ResponseEntity<Object>)new ResponseEntity((Object)this.pictureService.queryAll(criteria, pageable), HttpStatus.OK);
    }
    
    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping({ "/download" })
    @PreAuthorize("@el.check('pictures:list')")
    public void download(final HttpServletResponse response, final PictureQueryCriteria criteria) throws IOException {
        this.pictureService.download(this.generator.convert((List)this.pictureService.queryAll(criteria), (Class)PictureDto.class), response);
    }
    
    @Log("上传图片")
    @PreAuthorize("@el.check('pictures:add')")
    @PostMapping
    @ApiOperation("上传图片")
    public ResponseEntity<Object> upload(@RequestParam final MultipartFile file) {
        final String userName = SecurityUtils.getUsername();
        final Picture picture = this.pictureService.upload(file, userName);
        return (ResponseEntity<Object>)new ResponseEntity((Object)picture, HttpStatus.OK);
    }
    
    @Log("同步图床数据")
    @ApiOperation("同步图床数据")
    @PostMapping({ "/synchronize" })
    public ResponseEntity<Object> synchronize() {
        this.pictureService.synchronize();
        return (ResponseEntity<Object>)new ResponseEntity(HttpStatus.OK);
    }
    
    @Log("多选删除图片")
    @ApiOperation("多选删除图片")
    @PreAuthorize("@el.check('pictures:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody final Long[] ids) {
        this.pictureService.deleteAll(ids);
        return (ResponseEntity<Object>)new ResponseEntity(HttpStatus.OK);
    }
}
